﻿using System.Data;
using JKF.BusinessLayer.BLL;
using JKF.Entities;
using JKF.Entities.Model;
using JKF.Substructure;
using SFWS.XHandler.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JKF.HandlerLayer.Points
{
    public class Point_LogHandler : AHttpHandler
    {
        public override void ProcessRequest()
        {
            try
            {
                this.HandleRequest(Utils.GetActionName);
            }
            catch (Exception ex)
            {
                //将错误抛到客户端(以Json格式)
                Utils.ToClient(true, ex.Message);
                AppLogger.Log(ex.Message, ex);  //写日志
            }
        }

        /// <summary>
        /// 积分查询
        /// </summary>
        public void PointSearch()
        {
            bllPOINT_LOG bllPointLog=new bllPOINT_LOG();
            var obj = new PagerList();

            int pageCount = 0;
            int rowCount = 0;

            obj.Entity = from a in bllPointLog.PointSearch(P("searchItem"), P("matchWay"), P("keyword"), P("minPoint"), P("maxPoint"), P("ownShop"), P("PageIndex").ToInt32(), P("PageSize").ToInt32(), ref pageCount, ref rowCount).AsEnumerable()
                         select new
                         {
                             CUSTOMER_KEY = a.Field<Guid>("CUSTOMER_KEY").ToString(),
                             CARD_NO = a.Field<string>("CARD_NO"),
                             NAME = a.Field<string>("NAME"),
                             MOBILE = a.Field<string>("MOBILE"),
                             GIVE_POINTS = a.Field<Int32>("GIVE_POINTS").ToString(),
                             CONSUME_POINTS = a.Field<Int32>("CONSUME_POINTS").ToString(),
                             TOTAL_POINT = a.Field<Int32>("TOTAL_TRY_POINT").ToString()                             
                         };

            obj.RowCount = rowCount;
            obj.PageCount = pageCount;
            ResponseJsonObject = obj;
        }

        /// <summary>
        /// 浏览某个人的积分日志
        /// </summary>
        public void GetPointLogByPerson()
        {
            bllPOINT_LOG pointLog = new bllPOINT_LOG();
            var customerKey = P("customerKey");
            Response.Write(JsonSerializer.Serialize(pointLog.GetPointLogByPerson(customerKey), Utils.GetDateFormat));
        }

        /// <summary>
        /// 删除积分日志
        /// </summary>
        public void DeletePointLog()
        {
            try
            {
                var pointKey = P("pointKey");
                bllPOINT_LOG pointLog = new bllPOINT_LOG();
                pointLog.DeletePointLog(pointKey);
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = pointKey }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        public class POINT_LOG
        {
            public string CARD_NO;
            public string NAME;
            public string CODE;
            public string WAY;
            public string POINT_TIME;
            public string POINT_VALUE;
            public string MEMO;
            public string POINT_KEY;
            public string CUSTOMER_KEY;
        }

        public POINT_LOGinfo BuildSysModuleInfoByJson(POINT_LOG pointLogInfo)
        {
            var entity = new POINT_LOGinfo();

            if (!string.IsNullOrEmpty(pointLogInfo.POINT_KEY))
                entity.POINT_KEY =new Guid(pointLogInfo.POINT_KEY);

            entity.CUSTOMER_KEY = new Guid(pointLogInfo.CUSTOMER_KEY);
            entity.CODE = pointLogInfo.CODE;
            entity.WAY = pointLogInfo.WAY;
            entity.POINT_TIME =DateTime.Parse(pointLogInfo.POINT_TIME);
            entity.POINT_VALUE =Int32.Parse(pointLogInfo.POINT_VALUE);
            entity.MEMO = pointLogInfo.MEMO;
            return entity;
        }

        /// <summary>
        /// 添加或编辑积分
        /// </summary>
        public void EditOrInsertPoingLog()
        {
            try
            {
                bllPOINT_LOG bllPointLog = new bllPOINT_LOG();
                var strScr = P("pointLogObj");
                var pointLogInfo = BuildSysModuleInfoByJson(JsonSerializer.Deserialize<POINT_LOG>(strScr));
                var tid = pointLogInfo.POINT_KEY.ToString();
                var pk = bllPointLog.EditOrInsertPointLog(pointLogInfo);
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message = tid.IsGuidB() ? "编辑成功." : "添加成功.", Data = pk }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }
    }
}
